﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages
{
    public class PatientAll : BasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        //protected List<TopicInfo> TopicDataListHottest;
        //protected List<TopicInfo> TopicDataListLatest;

        // 最新加入的用户，如果浏览用户已经登录并且选择的疾病，则显示患同种疾病的最新加入的用户
        protected List<UserInfo> UserDataListLatest; 

        // 最活跃的用户，如果浏览用户已经登录并且选择了疾病，则显示患同种疾病的最新加入的用户
        protected List<UserInfo> PatientDataListActive;

        protected List<UserInfo> RecommendedUserList;
        protected List<UserInfo> PatientDataList;
        protected List<DiseaseInfo> DiseaseLst;
        protected List<TreatmentInfo> TreatmentLst;
        protected List<SymptomInfo> SymptomLst;
        protected List<DistrictInfo> ProvinceList;
        protected int PatientCount = 0;
        protected int PatientPageCount = 0;
        protected int PatientPageSize = 15;
        protected int PatientPageIndex = 0;
        protected string PatientPagingString;

        protected List<LogTopicInfo> HottestLogTopics;

        protected void HandleCommon()
        {
            this.SetTitle("病友");
            this.AddLocation("病友列表", "/DiseaseList.aspx");
            this.AddLinkCss("/support/alpha/css/PatientAll.css");
            DiseaseLst = DiseaseBLL.Instance.GetAllDiseaseByName("");
            TreatmentLst = TreatmentBLL.Instance.GetAllTreatmentByName("");
            SymptomLst = SymptomBLL.Instance.GetAllSymptomByName("");
            ProvinceList = DistrictBLL.Instance.GetDistrictListByParent(0);
        }

        protected void HandlePost()
        {
        }

#if GENDATA
        protected void HandleGet()
        {
            TopicDataListHottest = new List<TopicInfo>();
            for (int i = 0; i < 5; i++)
            {
                TopicInfo info = new TopicInfo();
                info.Id = i;
                info.Title = "最热话题最热话题";
                info.ReplyCount = 10;
                TopicDataListHottest.Add(info);
            }

            TopicDataListLatest = new List<TopicInfo>();
            for (int i = 0; i < 5; i++)
            {
                TopicInfo info = new TopicInfo();
                info.Id = i;
                info.Title = "最新话题最新话题";
                info.ReplyCount = 10;
                TopicDataListLatest.Add(info);
            }

            UserDataListLatest = new List<UserInfo>();
            for (int i = 0; i < 5; i++)
            {
                UserInfo info = new UserInfo();
                info.Id = i;
                info.Avatar = "/support/dnt/avatars/common/1.jpg";
                info.UserName = "用户名";
                UserDataListLatest.Add(info);
            }
            PatientPageIndex = RequestTools.GetQueryInt("page", 1);

            PatientDataList = new List<UserInfo>();
            for (int i = 1; i < 10; i++)
            {
                UserInfo info = new UserInfo();
                info.Id = 100 * i + i;
                info.UserName = "病人";
                info.Intro = "病人简介病人简介病人简介病人简介病人简介病人简介病人简介";
                info.Avatar = "/support/dnt/avatars/common/1.jpg";
                info.Age = 20;
                info.Gender = "男";
                info.DiseaseId = 10;
                info.DiseaseName = "糖尿病";
                PatientDataList.Add(info);
            }
            PatientPageCount = Utils.GetPageCount(PatientCount, 10);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PatientPagingString = this.GetPagingString(PatientPageIndex, PatientPageCount,
                "/PatientList.aspx" + query, "page");

        }

#else
        protected void HandleGet()
        {
            int count = 6;
            //TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(count);
            //TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(count);

            //  -------------- 获取推荐用户信息 -----------------
            int diseaseId = 0;
            string diseaseName = "";
            if (UserData.Id > 0)
            {
                PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
                if (prData.DiseaseId > 0)
                {
                    UserData.DiseaseId = prData.DiseaseId;
                    diseaseId = prData.DiseaseId;
                    diseaseName = DiseaseBLL.Instance.GetDiseaseInfo(diseaseId).Name;
                }
            }

            
            RecommendedUserList = UserBLL.Instance.GetRecommendedUserListByDiease(diseaseName);


            if ((RecommendedUserList.Count == 0))
            {
                RecommendedUserList = UserBLL.Instance.GetRecommendedUserListByDiease("其他");
            }

            for (int index = 0; index < RecommendedUserList.Count; index++)
            {
                int userId = RecommendedUserList[index].Id;
                PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(userId);
                RecommendedUserList[index].DiseaseId = prData.DiseaseId;
                RecommendedUserList[index].DiseaseName = DiseaseBLL.Instance.GetDiseaseInfo(prData.DiseaseId).Name;
            }
            //  --------------------------------------------------

            if (diseaseId > 0)
            {
                UserDataListLatest = PatientBLL.Instance.GetLatestPatientByDisease(count, diseaseId);
                PatientDataListActive = PatientBLL.Instance.GetPatientDataListActiveByDisease(count, diseaseId);
            }
            else
            {
                UserDataListLatest = PatientBLL.Instance.GetLastestPatient(count);
                PatientDataListActive = PatientBLL.Instance.GetPatientDataListActive(count);
            }

            int logTopicsCount = 8;
            if (diseaseId > 0)
            {
                HottestLogTopics = LogTopicBLL.Instance.GetHottestLogTopicListByDisease(logTopicsCount, diseaseId);
            }
            else
            {
                HottestLogTopics = LogTopicBLL.Instance.GetHottestLogTopicList(logTopicsCount);
            }

            PatientSearchCondition cond = new PatientSearchCondition();
            PatientPageIndex = RequestTools.GetQueryInt("page", 1);

            PatientDataList = PatientBLL.Instance.GetPatientList(
                PatientPageSize, PatientPageIndex,
                cond, out PatientCount);

            PatientPageCount = Utils.GetPageCount(PatientCount, PatientPageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PatientPagingString = this.GetPagingString(PatientPageIndex, PatientPageCount,
                "/PatientAll.aspx" + query, "page");
        }
#endif
    }
}